package com.xiayuanxing.arithmetic.Kmp;

/**
 * @projectName: arithmetic
 * @className: ViolenceMath
 * @author: xiayuanxing
 * @description: 暴力匹配算法
 * @date: 2021/11/4 10:26
 * @version: 1.0
 */
public class ViolenceMath {

    public static void main(String[] args) {

        //测试
        String s1 = "暴力匹配算法 夏远~兴暴力匹 配算法暴力 匹配算法";
        String s2 = "夏远兴";
        int i = violenceMath(s1, s2);
        System.out.println(i);

    }

    /**
     * @param str1:
     * @param str2:
     * @return int
     * @author xiayuanxing
     * @description 暴力匹配算法
     * @date 2021/11/4 10:31
     */
     public static int violenceMath(String str1,String str2){

         char[] s1 = str1.toCharArray();
         char[] s2 = str2.toCharArray();

         int s1Length = s1.length;
         int s2Length = s2.length;
         //i索引指向s1
         int i = 0;
         //j索引指向s2
         int j = 0;

         while (i < s1Length && j < s2Length){
             if (s1[i] == s2[j]){
                 //说明匹配到了
                 i++;
                 j++;
             }else{
                 //没有匹配到
                 i = i -(j - 1);
                 j = 0;
             }
         }
         //判断是否匹配成功
         if (j == s2Length){
             return i - j;
         }else{
             return -1;
         }

     }
}
